package cn.le.service;

import cn.le.dao.ProductDAO;
import cn.le.dto.ProductDTO;
import cn.le.po.ProductPO;
import cn.le.util.BeanUtils;
import cn.le.util.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ProductService {

    @Autowired
    private ProductDAO productDAO;

    @Transactional(rollbackFor = Exception.class)
    public void add(ProductDTO dto){
        ProductPO po = productDAO.getByName(dto.getName());
        if (po != null){
            throw new RuntimeException("产品重复");
        }
        po = BeanUtils.copyProperties(ProductPO.class,dto);
        po.setId(IdUtils.next());
        po.setFormat(dto.getFormat().getFormat());
        po.setProtocol(dto.getProtocol().getProtocol());
        po.setCreateAt(new Date());
        productDAO.insert(po);
    }

    public void isProduct(Long id){
        ProductPO productPO = productDAO.getById(id);
        if (productPO == null) {
            throw new RuntimeException("产品不存在");
        }
    }

}
